#include "EditorSettingsPage.h"
#include "ui_EditorSettingsPage.h"
#include <QColorDialog>

EditorSettingsPage::EditorSettingsPage(QWidget* parent)
    : AbstractSettingsPage(parent)
    , ui(new Ui::EditorSettingsPage)
{
    ui->setupUi(this);
    load();
}

EditorSettingsPage::~EditorSettingsPage()
{
    delete ui;
}

void EditorSettingsPage::changeEvent(QEvent* event)
{
    QWidget::changeEvent(event);
    switch (event->type()) {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        break;
    default:
        break;
    }
}

void EditorSettingsPage::load()
{
    QSettings settings;
    _fontSettings.fromSettings(&settings);
    _tabSettings.fromSettings(&settings);
    _displaySettings.fromSettings(&settings);
    _storageSettings.fromSettings(&settings);

    // font
    ui->cbbFamily->setCurrentFont(_fontSettings.font());
    ui->cbbSize->setFontSize(_fontSettings.fontSize());

    // color
    ui->btnLineNumbersColor->setColor(_displaySettings.lineNumbersColor);
    ui->btnLineNumbersFillColor->setColor(_displaySettings.lineNumbersFillColor);
    ui->btnFoldingMarkersFillColor->setColor(_displaySettings.foldingMarkersFillColor);
    ui->btnCurrentLineFillColor->setColor(_displaySettings.currentLineFillColor);
    ui->btnWrapEdgeColor->setColor(_displaySettings.wrapEdgeColor);
    ui->btnWrapEdgeFillColor->setColor(_displaySettings.wrapEdgeFillColor);

    // tab
    ui->chkSpacesForTabs->setChecked(_tabSettings.spacesForTabs);
    ui->spbTabSize->setValue(_tabSettings.tabSize);
    ui->chkAutoIndent->setChecked(_tabSettings.autoIndent);
    ui->spbIndentSize->setValue(_tabSettings.indentSize);
    ui->chkSmartBackspace->setChecked(_tabSettings.smartBackspace);
    ui->cbbTabKeyBehavior->clear();
    ui->cbbTabKeyBehavior->addItem(tr("Never"));
    ui->cbbTabKeyBehavior->addItem(tr("Always"));
    ui->cbbTabKeyBehavior->addItem(tr("In leading white space"));
    ui->cbbTabKeyBehavior->setCurrentIndex(CodeEdit::TabSettings::TabAlwaysIndents);

    // storage
    ui->chkCleanWhitespace->setChecked(_storageSettings.cleanWhitespace);
    ui->chkInEntireDocument->setChecked(_storageSettings.inEntireDocument);
    ui->chkCleanIndentation->setChecked(_storageSettings.cleanIndentation);
    ui->chkAddFinalNewline->setChecked(_storageSettings.addFinalNewLine);
}

void EditorSettingsPage::save()
{
    // font
    _fontSettings.setFontFamily(ui->cbbFamily->currentText());
    _fontSettings.setFontSize(ui->cbbSize->fontSize());

    // color
    _displaySettings.lineNumbersColor = ui->btnLineNumbersColor->color();
    _displaySettings.lineNumbersFillColor = ui->btnLineNumbersFillColor->color();
    _displaySettings.foldingMarkersFillColor = ui->btnFoldingMarkersFillColor->color();
    _displaySettings.currentLineFillColor = ui->btnCurrentLineFillColor->color();
    _displaySettings.wrapEdgeColor = ui->btnWrapEdgeColor->color();
    _displaySettings.wrapEdgeFillColor = ui->btnWrapEdgeFillColor->color();

    // tab
    _tabSettings.spacesForTabs = ui->chkSpacesForTabs->isChecked();
    _tabSettings.tabSize = ui->spbTabSize->value();
    _tabSettings.autoIndent = ui->chkAutoIndent->isChecked();
    _tabSettings.indentSize = ui->spbIndentSize->value();
    _tabSettings.smartBackspace = ui->chkSmartBackspace->isChecked();
    _tabSettings.tabKeyBehavior = (CodeEdit::TabSettings::TabKeyBehavior) ui->cbbTabKeyBehavior->currentIndex();

    // storage
    _storageSettings.cleanWhitespace = ui->chkCleanWhitespace->isChecked();
    _storageSettings.inEntireDocument = ui->chkInEntireDocument->isChecked();
    _storageSettings.cleanIndentation = ui->chkCleanIndentation->isChecked();
    _storageSettings.addFinalNewLine = ui->chkAddFinalNewline->isChecked();

    QSettings settings;
    _fontSettings.toSettings(&settings);
    _tabSettings.toSettings(&settings);
    _displaySettings.toSettings(&settings);
    _storageSettings.toSettings(&settings);
}

void EditorSettingsPage::changeColor()
{
    ColorButton* btn = qobject_cast<ColorButton*>(sender());
    if (btn) {
        QColor color = QColorDialog::getColor(btn->color(), this);
        if (color.isValid()) {
            btn->setColor(color);
        }
    }
}
